package com.gopay.bis.service.bank.impl.bcdl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.manager.bank.bcdl.PabBcdlManager;
import com.gopay.common.constants.bank.PabConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BcdlMessage;
import com.gopay.common.domain.bank.BcdlResult;
import com.gopay.common.domain.bank.bcdl.BcdlPacket;
import com.gopay.common.domain.bank.bcdl.BcdlPacketRes;
import com.gopay.common.domain.bank.pab.bcdl.PABOnlineHeaderMessage;
import com.gopay.common.domain.bank.pab.bcdl.res.ResPAB4004;
import com.gopay.common.domain.bank.pab.bcdl.res.ResPAB4005;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BcdlInteractRemoteService;

/**
 * 平安银企直连
 * 
 * @author WangHongFei
 * 
 */
public class PabBcdlServiceImpl implements BcdlInteractRemoteService {

    private static Logger logger = LoggerFactory.getLogger(PabBcdlServiceImpl.class);
    @Resource(name = "pabBcdlManager")
    PabBcdlManager pabBcdlManager;

    @Override
    public BcdlResult eft(BcdlMessage base) {
        BcdlResult res = new BcdlResult();
        try {

            BcdlMessage payReq = base;
            ResPAB4004 bankResult = (ResPAB4004) pabBcdlManager.eft(payReq);
            logger.error("PAB-4004回应解析：" + bankResult);
            PABOnlineHeaderMessage resHeader = bankResult.getHeader();
            if (PabConstants.SDB_ONLINE_RETCD_000000.equals(resHeader.getRetCd())) {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
                res.setSuccess(true);
            } else if (PabConstants.SDB_ONLINE_RETCD_MA9112.equals(resHeader.getRetCd())) {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
            } else {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
            }
            // 非成功状态，赋值银行处理结果
            if (!PabConstants.SDB_ONLINE_RETCD_000000.equals(resHeader.getRetCd())) {
                res.setSuccess(false);
                res.setBankRetMsg(resHeader.getRemark());
            }
        } catch (GopayException e) {
            logger.error("PAB银企直连异常 eft", e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100E6045);
            res.setSuccess(false);
        } catch (Exception e) {
            logger.error("PAB银企直连异常 eft", e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100E6045);
            res.setSuccess(false);
        }
        return res;
    }

    @Override
    public BcdlResult query(BcdlMessage base) {
        BcdlResult res = new BcdlResult();
        try {
            BcdlMessage req = base;
            ResPAB4005 res4005 = (ResPAB4005) pabBcdlManager.query(req);
            PABOnlineHeaderMessage resHeader = res4005.getHeader();
            if (PabConstants.SDB_ONLINE_RETCD_000000.equals(resHeader.getRetCd())) {
                if ("20".equals(res4005.getStt())) {
                    res.setSuccess(true);
                    res.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);// 成功
                } else if ("30".equals(res4005.getStt())) {
                    res.setSuccess(false);
                    res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);// 失败
                } else {
                    res.setSuccess(false);
                    res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);// 处理中
                }
            } else if (PabConstants.SDB_ONLINE_RETCD_MA0103.equals(resHeader.getRetCd())) {
                res.setBankRetMsg(resHeader.getRemark());
                res.setSuccess(false);
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6001);// 记录不存在
            } else {
                res.setBankRetMsg(resHeader.getRemark());
                res.setSuccess(false);
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);// 状态不明
            }
            // 数据返回客户端
            if (ProcCodeConstants.PROC_CODE_100S1000.equals(res.getResultCode())) {
                res.setDefaultModel(res4005);
            }
        } catch (GopayException e) {
            logger.error("PAB银企直连异常 query", e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100E6050);
            res.setSuccess(false);
        } catch (Exception e) {
            logger.error("PAB银企直连异常 query", e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100E6050);
            res.setSuccess(false);
        }
        return res;
    }

    @Override
    public BcdlResult sendBank(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult queryBalance(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult sendBankQueryBalance(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult eftRetry(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult sendBankPacket(BcdlPacket bcdlPacket) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlPacketRes queryPacket(BcdlPacket bcdlPacket) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult queryAcctDetail(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

}